# coding=utf-8
import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference
import mnist_train

EVAL_INTERVAL_SECS = 10


def evaluate(mnist):
    with tf.Graph().as_default() as g:
        x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input')
        y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input')

        validate_feed = {x: mnist.test.images, y_: mnist.test.labels}

        y = mnist_inference.inference(x, None)

        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))

        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        variables_averages = tf.train.ExponentialMovingAverage(mnist_train.MOVING_AVERAGE_DECAY)
        variables_to_restore = variables_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(mnist_train.CKPT_PATH)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    global_step = ckpt.model_checkpoint_path.split("/")[-1].split("-")[-1]
                    accuracy_score = sess.run(accuracy, feed_dict=validate_feed)
                    print(accuracy_score, global_step)
                    for index in range(100):
                        print("=====")
                        print(sess.run(y, feed_dict={x: [mnist.test.images[index]]}))
                        print(mnist.test.labels[index])
                else:
                    print("None")
                    return
                time.sleep(EVAL_INTERVAL_SECS)


def test(mnist):
    with tf.Graph().as_default() as g:
        x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input')
        y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input')

        y = mnist_inference.inference(x, None)
        variables_averages = tf.train.ExponentialMovingAverage(mnist_train.MOVING_AVERAGE_DECAY)
        variables_to_restore = variables_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)
        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(mnist_train.CKPT_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                for index in range(200,300):
                    print("=====")
                    test = tf.argmax(y, 1)[0]
                    real = tf.argmax(y_, 1)[0]

                    if type(test):
                        test_value = sess.run(test, feed_dict={x: [mnist.test.images[index]]})
                        real_value = sess.run(real, feed_dict={y_: [mnist.test.labels[index]]})
                        if test_value != real_value:
                            print(sess.run(test, feed_dict={x: [mnist.test.images[index]]}))
                            print(sess.run(real, feed_dict={y_: [mnist.test.labels[index]]}))


def main(argv=None):
    mnist = input_data.read_data_sets(mnist_inference.MNIST_PATH, one_hot=True)
    test(mnist)


if __name__ == '__main__':
    tf.app.run()
